﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Project.Library
{
    public class Local : IFile
    {
        public int Init(String ConnectionString)
        {
            try
            {
                // Can Write Code To Check If there is Write Permission For File 
            }
            catch(Exception e)
            {
                throw e;
            }
            return 1;
        }

        public String Read(String FileName)
        {
            try
            {
                using (StreamReader oStreamReader = new System.IO.StreamReader(FileName))
                {
                    return oStreamReader.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public int Write(String FileName,String Data)
        {
            try
            {
                using (StreamWriter oStreamWriter = new System.IO.StreamWriter(FileName))
                {
                    oStreamWriter.Write(Data);
                    return 1;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public int Write(String FileName, Stream stream)
        {
            try
            {
                using (StreamWriter oStreamWriter = new System.IO.StreamWriter(FileName))
                {
                    oStreamWriter.Write(stream);
                    return 1;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public int Delete(String FileName)
        {
            try
            {
                if (System.IO.File.Exists(FileName))
                {
                    System.IO.File.Delete(FileName);
                    return 1;
                }
                else
                {
                    return 2;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool Exists(String FileName)
        {
            if(System.IO.File.Exists(FileName))
            {
                return true;
            }else
            {
                return false;
            }
        }

        public int CreateFolder(String FolderName)
        {
            try
            {
                System.IO.Directory.CreateDirectory(FolderName);
                return 1;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public int DeleteFolder(String FolderName)
        {
            try
            {
                System.IO.Directory.Delete(FolderName);
                return 1;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool ExistsFolder(String FolderName)
        {
            try
            {

                if (System.IO.Directory.Exists(FolderName))
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception e)
            {
                throw e;
            }
        }


    }
}
